// 实现这个项目的构建任务
const { src, dest, parallel, series, watch } = require('gulp')
const del = require('del')

//创建开发服务器
const browserSync = require('browser-sync')
const bs = browserSync.create()

//自动加载 gulp 插件
const loadPlugins = require('gulp-load-plugins')
const plugins = loadPlugins()

const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

//clear dist 任务
const clean = () => {
  return del(['dist', 'temp'])
}

//样式编译
const style = () => {
  return src('src/assets/styles/*.scss', { base: 'src' })
    .pipe(plugins.sass({ outputStyle: 'expanded' }))
    .pipe(dest('temp'))
    .pipe(bs.reload({ stream: true })) //以流的方式推到浏览器
}

//脚本编译
const script = () => {
  return src('src/assets/scripts/*.js', { base: 'src' })
    .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
    .pipe(dest('temp'))
    .pipe(bs.reload({ stream: true }))
}

//页面编译
const page = () => {
  return src('src/*.html', { base: 'src' })
    .pipe(plugins.swig({ data, defaults: { cache: false } })) //防止页面缓存不刷新
    .pipe(dest('temp'))
    .pipe(bs.reload({ stream: true }))
}

//图片转换
const image = () => {
  return src('src/assets/images/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

// 字体转换
const font = () => {
  return src('src/assets/fonts/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

//public中不需要转换的其他文件直拷贝
const extra = () => {
  return src('public/**', { base: 'public' }).pipe(dest('dist'))
}

//开发服务器任务以及配置
const serve = () => {
  //监听源文件变化执行相应的任务
  watch('src/assets/styles/*.scss', style)
  watch('src/assets/scripts/*.js', script)
  watch('src/*.html', page)
  //图片、字体、静态文件不参与构建，监听到变化用bs.reload 重新更新浏览器
  watch(['src/assets/images/**', 'src/assets/fonts/**', 'public/**'], bs.reload)

  bs.init({
    notify: false, //关闭启动时的提示信息弹窗
    port: 8080, //端口号
    // open: false,//启动后是否自动打开
    // files: 'dist/**', //监听dist文件变化,常规使用是不在这里监听，在各个任务中再 pipe 到 bs.reload
    server: {
      baseDir: ['temp', 'src', 'public'], //加入src public，减少构建请求
      routes: {
        '/node_modules': 'node_modules', //路由映射
      },
    },
  })
}

//useref 任务，处理文件引用路径、压缩文件
const useref = () => {
  return (
    src('temp/*.html', { base: 'temp' })
      .pipe(plugins.useref({ searchPath: ['temp', '.'] }))
      //压缩js,css,html
      .pipe(plugins.if(/\.js$/, plugins.uglify()))
      .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
      .pipe(
        plugins.if(
          /\.html$/,
          plugins.htmlmin({
            collapseWhitespace: true,
            minifyCSS: true,
            minifyJS: true,
          })
        )
      )
      .pipe(dest('dist'))
  )
}

//创建组合任务
const compile = parallel(style, script, page)
const build = series(
  clean,
  parallel(series(compile, useref), extra, image, font)
)
const develop = series(compile, serve)

module.exports = {
  clean,
  build,
  develop,
}